package com.iplant.mes.controller.exc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.EXCService;
import com.iplant.mes.service.mesenum.exc.EXCLineStopStatus;
import com.iplant.mes.service.po.exc.EXCLineStop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/api/EXCLineStop")
public class EXCLineStopController extends BasicController<EXCLineStop> {

    public EXCLineStopController() {
        super(EXCLineStop.class);
    }

    @Autowired
    EXCService mEXCService;

    /**
     * 提交
     */
    @PostMapping("/Submit")
    public Object Submit(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            EXCLineStop wEXCLineStop = CloneTool.Clone(wParam.get("data"), EXCLineStop.class);
            int wStatus = StringUtils.parseInt(wParam.get("Status"));
            if (wEXCLineStop == null || EXCLineStopStatus.getEnumType(wStatus).getValue() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mEXCService.EXC_SubmitLineStop(wLoginUser, wEXCLineStop, wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("Submit", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
